package com.ybl.store.backend.controller.deal;

import com.alibaba.excel.EasyExcel;
import com.ybl.store.backend.dto.req.deal.LogDealProductNumberSaveReqDTO;
import com.ybl.store.backend.dto.req.deal.LogDealProductNumberUpdateReqDTO;
import com.ybl.store.backend.dto.resp.deal.LogDealProductNumberExportRespDTO;
import com.ybl.store.backend.dto.resp.deal.LogDealProductNumberPageRespDTO;
import com.ybl.store.backend.service.deal.LogDealProductNumberService;
import com.ybl.store.common.convention.errorcode.BaseErrorCode;
import com.ybl.store.common.convention.exception.ClientException;
import com.ybl.store.common.convention.result.Result;
import com.ybl.store.common.convention.result.Results;
import com.ybl.store.common.entity.ListRespDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/backend/log/deal/product-number")
public class LogDealProductNumberController {
    
    private final LogDealProductNumberService logDealProductNumberService;

    @PostMapping
    public Result<Void> saveLogDealProductNumber(@RequestBody LogDealProductNumberSaveReqDTO requestParam){
        logDealProductNumberService.saveLogDealProductNumber(requestParam);
        return Results.success();
    }

    @DeleteMapping
    public Result<Void> deleteLogDealProductNumber(@RequestParam Long id) {
        logDealProductNumberService.deleteLogDealProductNumber(id);
        return Results.success();
    }

    @PutMapping
    public Result<Void> updateLogDealProductNumber(@RequestBody LogDealProductNumberUpdateReqDTO requestParam) {
        logDealProductNumberService.updateLogDealProductNumber(requestParam);
        return Results.success();
    }

    @GetMapping("/page")
    public Result<ListRespDTO<LogDealProductNumberPageRespDTO>> pageLogDealProductNumber(
            @RequestParam Integer pn,
            @RequestParam Integer ps,
            @RequestParam(required = false) Long dealProductId,
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) Long clerkId,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime)
    {
        return Results.success(logDealProductNumberService.pageLogDealProductNumber(pn, ps, dealProductId, storeId, clerkId, customerId, productId, startTime, endTime));
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=data.xlsx");

        try {
            List<LogDealProductNumberExportRespDTO> export = logDealProductNumberService.export();
            EasyExcel.write(response.getOutputStream(), LogDealProductNumberExportRespDTO.class)
                    .sheet("data")
                    .doWrite(export);
        }
        catch (IOException e) {
            throw new ClientException(BaseErrorCode.EXCEL_ERROR);
        }
    }
}
